package com.example.crowdfunding.mvc.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/* *
 * 访问目标资源时验证是否登录的拦截器，注意，此类需要在mvc的配置文件中注册
 */
public class LoginIntercept extends HandlerInterceptorAdapter {

    /**
     * 验证是否登录
     *
     * @param request  request
     * @param response response
     * @param handler  handler
     * @return true or false
     * @throws Exception exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // todo 去除了登录检查
//        // 1.通过request获得session
//        HttpSession session = request.getSession();
//
//        // 2.获取session中的admin
//        Admin admin = (Admin) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
//
//        if (admin == null) {
//            // 2.1没获取到，抛出异常
//            throw new AccessForbiddenException(CrowdConstant.MESSAGE_LOGIN_ACCESS_FORBIDDEN);
//        }
        // 2.2获取到了
        return true;
    }

}
